#pragma once

#include "imconfig.h"
#include "imgui.h"
#include "imgui_impl_dx11.h"
#include "imgui_impl_win32.h"
#include "imgui_internal.h"
#include "imstb_rectpack.h"
#include "imstb_textedit.h"
#include "imstb_truetype.h"
#include "Font.h"

#include <string.h>
#include <tchar.h>
#include <chrono>
#include <thread>
#include <d3d11.h>
#pragma comment(lib,"d3d11.lib")
#pragma warning( disable : 4996 )

// 项目专用函数库
#include "CodeSharingLibrary.h"
#include "GetEnvironmentCommon.h"
#include "GetDownloadCommon.h"
#include "GetSetSystemCommon.h"
#include "UI_SetUI.h"





static ID3D11Device* g_pd3dDevice = NULL;
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
static IDXGISwapChain* g_pSwapChain = NULL;
static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;

bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);


void Helpmarker(const char* Text, ImVec4 Color) {
    ImGui::TextColored(Color, "(?)");
    if (ImGui::IsItemHovered())
        ImGui::SetTooltip(Text);
}


int main(int, char**) {
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("新大学生工具箱"), NULL };
    ::RegisterClassEx(&wc);
    HWND hwnd = ::CreateWindow(wc.lpszClassName, _T("新大学生工具箱"), WS_OVERLAPPEDWINDOW, 100, 100, 1, 1, NULL, NULL, wc.hInstance, NULL);

    if (!CreateDeviceD3D(hwnd)) {
        CleanupDeviceD3D();
        ::UnregisterClass(wc.lpszClassName, wc.hInstance);
        return 1;
    }

    ::ShowWindow(hwnd, SW_HIDE);
    ::UpdateWindow(hwnd);

    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
    io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
    io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;

    ImGui::StyleColorsDark();

    ImGui_ImplWin32_Init(hwnd);
    ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);

    io.IniFilename = nullptr;
    ImFontConfig Font_cfg;
    Font_cfg.FontDataOwnedByAtlas = false;

    float ScalingRatio = GetScreenScalingRatio(); // 屏幕缩放率
    int Rate_Hz = GetRefreshRateAPI(); // 屏幕刷新率
    int StandardTargetFrameTimeMs = 1000 / Rate_Hz; // 标准刷新时间间隔（delta）
    int TargetFrameTimeMs = StandardTargetFrameTimeMs * 2; // 校正程序间隔识别（!! 模拟垂直同步系列代码有点问题，需要修复 !!）
    Timer timer; // 高精度计时器类

    //ImFont* Font = io.Fonts->AddFontFromFileTTF("..\\新大学生工具箱\\Font.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
    ImFont* Font = io.Fonts->AddFontFromMemoryTTF((void*)Font_data, Font_size, 18.0f * ScalingRatio, &Font_cfg, io.Fonts->GetGlyphRangesChineseFull());
    ImFont* Font_Big = io.Fonts->AddFontFromMemoryTTF((void*)Font_data, Font_size, 24.0f * ScalingRatio, &Font_cfg, io.Fonts->GetGlyphRangesChineseFull());
    ImFont* Font_Title = io.Fonts->AddFontFromMemoryTTF((void*)Font_data, Font_size, 29.0f * ScalingRatio, &Font_cfg, io.Fonts->GetGlyphRangesChineseFull());

    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    bool done = false;

    // MinGW 区
    // CDE_A 返回表数值含义：
    // 1 - 环境未安装
    // 2 - 相同版本的环境已安装
    // 3 - 不同版本的环境已安装
    // 4 - 环境有无情况判断异常
    bool MinGW_CDE_A_1 = false, MinGW_CDE_A_2 = false, MinGW_CDE_A_3 = false, MinGW_CDE_A_4 = false;

    // CDE_B 返回表数值含义：
    // 0 - 存储空间不足
    // 1 - 安装到 D:\
    // 2 - 安装到 C:\
    // 3 - 文件MD5不符合预设
    // 4 - 文件（环境文件压缩包）不存在
    bool MinGW_CDE_B_0 = false, MinGW_CDE_B_3 = false, MinGW_CDE_B_4 = false;

    // In_UnZip 返回表数值含义：
    // 0 - 解压失败
    // 1 - 解压成功
    bool MinGW_In_UnZip_0 = false;

    // CDE_CF 返回表数值含义：
    // 0 - 环境变量设置失败
    // 1 - 环境变量设置成功，成功配置环境
    bool MinGW_CDE_CF_0 = false, MinGW_CDE_CF_1 = false;

    // Python 区
    // CDE_A 返回表数值含义：
    // 1 - 环境未安装
    // 2 - 相同版本的环境已安装
    // 3 - 不同版本的环境已安装
    // 4 - 环境有无情况判断异常
    bool Python_CDE_A_1 = false, Python_CDE_A_2 = false, Python_CDE_A_3 = false, Python_CDE_A_4 = false;

    // CDE_B 返回表数值含义：
    // 0 - 存储空间不足
    // 1 - 安装到 D:\
    // 2 - 安装到 C:\
    // 3 - 文件MD5不符合预设
    // 4 - 文件（环境文件压缩包）不存在
    bool Python_CDE_B_0 = false, Python_CDE_B_3 = false, Python_CDE_B_4 = false;

    // In_UnZip 返回表数值含义：
    // 0 - 解压失败
    // 1 - 解压成功
    bool Python_In_UnZip_0 = false;

    // CDE_CF 返回表数值含义：
    // 0 - 环境变量设置失败
    // 1 - 环境变量设置成功，成功配置环境
    bool Python_CDE_CF_0 = false, Python_CDE_CF_1 = false;

    // Java 区
    // CDE_A 返回表数值含义：
    // 1 - 环境未安装
    // 2 - 相同版本的环境已安装
    // 3 - 不同版本的环境已安装
    // 4 - 环境有无情况判断异常
    bool Java_CDE_A_1 = false, Java_CDE_A_2 = false, Java_CDE_A_3 = false, Java_CDE_A_4 = false;

    // CDE_B 返回表数值含义：
    // 0 - 存储空间不足
    // 1 - 安装到 D:\
    // 2 - 安装到 C:\
    // 3 - 文件MD5不符合预设
    // 4 - 文件（环境文件压缩包）不存在
    bool Java_CDE_B_0 = false, Java_CDE_B_3 = false, Java_CDE_B_4 = false;

    // In_UnZip 返回表数值含义：
    // 0 - 解压失败
    // 1 - 解压成功
    bool Java_In_UnZip_0 = false;

    // CDE_CF 返回表数值含义：
    // 0 - 环境变量设置失败
    // 1 - 环境变量设置成功，成功配置环境
    bool Java_CDE_CF_0 = false, Java_CDE_CF_1 = false;

    // GoLang 区
    // CDE_A 返回表数值含义：
    // 1 - 环境未安装
    // 2 - 相同版本的环境已安装
    // 3 - 不同版本的环境已安装
    // 4 - 环境有无情况判断异常
    bool GoLang_CDE_A_1 = false, GoLang_CDE_A_2 = false, GoLang_CDE_A_3 = false, GoLang_CDE_A_4 = false;

    // CDE_B 返回表数值含义：
    // 0 - 存储空间不足
    // 1 - 安装到 D:\
    // 2 - 安装到 C:\
    // 3 - 文件MD5不符合预设
    // 4 - 文件（环境文件压缩包）不存在
    bool GoLang_CDE_B_0 = false, GoLang_CDE_B_3 = false, GoLang_CDE_B_4 = false;

    // In_UnZip 返回表数值含义：
    // 0 - 解压失败
    // 1 - 解压成功
    bool GoLang_In_UnZip_0 = false;

    // CDE_CF 返回表数值含义：
    // 0 - 环境变量设置失败
    // 1 - 环境变量设置成功，成功配置环境
    bool GoLang_CDE_CF_0 = false, GoLang_CDE_CF_1 = false;

    // 下载程序 - 打开官网中的自动下载链接
    // Success - 成功
    // Fail - 失败
    bool Download_Success = false, Download_Fail = false;

    // 长路径支持 - 类似于安装Python所选择的长路径限制解除
    // ON - 开启长路径支持
    // OFF - 关闭长路径支持
    // Success - 成功
    // Fail - 失败
    bool SetLongPath_ON_Success = false, SetLongPath_ON_Fail = false, SetLongPath_OFF_Success = false, SetLongPath_OFF_Fail = false;

    // 解压等待窗口
    bool MinGW_UnZip_Wait = false, Python_UnZip_Wait = false, Java_UnZip_Wait = false, GoLang_UnZip_Wait = false;

    // 在全局或类作用域内声明互斥锁（根据变量作用域调整）
    std::mutex mingwMutex, PythonMutex, JavaMutex, GoLangMutex;

    // 使用steady_clock获取当前时间点作为基准 - 限制刷新率，即性能优化
    auto next_frame_time = std::chrono::steady_clock::now();
    while (!done) {
        //开始的时间 - 降低性能占用
        next_frame_time += std::chrono::milliseconds(TargetFrameTimeMs); // 计算下一帧的目标时间点（绝对时间） - 降低性能占用

        MSG msg;
        while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
            if (msg.message == WM_QUIT)
                done = true;
        }
        if (done)
            break;

        ImGui_ImplDX11_NewFrame();
        ImGui_ImplWin32_NewFrame();
        ImGui::NewFrame();

        {
            ImGuiStyle& Style = ImGui::GetStyle();
            auto Color = Style.Colors;

            static bool WinPos = true;//用于初始化窗口位置
            int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
            int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

            static bool CheckBox_1 = false, CheckBox_2 = true;
            static int InputInt = 0;
            static int Comb = 0;
            static float InputFloat = 0;
            static char InputString[80] = { '?' };

            static int Tab = 0;
            enum Tab {
                SetDevEnv,
                Download,
                SetSystem,
                SetToolbox
            };

            static int Color_ = 0;
            enum Color_ {
                Red,
                Green,
                Blue,
                Orange
            };

            switch (Color_) {
            case Color_::Red:
                Style.ChildRounding = 8.0f;
                Style.FrameRounding = 5.0f;

                Color[ImGuiCol_Button] = ImColor(239, 75, 82, 255);
                Color[ImGuiCol_ButtonHovered] = ImColor(212, 106, 129, 255);
                Color[ImGuiCol_ButtonActive] = ImColor(172, 31, 54,255);

                Color[ImGuiCol_FrameBg] = ImColor(54, 54, 54, 150);
                Color[ImGuiCol_FrameBgActive] = ImColor(42, 42, 42, 150);
                Color[ImGuiCol_FrameBgHovered] = ImColor(100, 100, 100, 150);

                Color[ImGuiCol_CheckMark] = ImColor(192, 51, 74, 255);

                Color[ImGuiCol_SliderGrab] = ImColor(192, 51, 74, 255);
                Color[ImGuiCol_SliderGrabActive] = ImColor(172, 31, 54, 255);

                Color[ImGuiCol_Header] = ImColor(192, 51, 74, 255);
                Color[ImGuiCol_HeaderHovered] = ImColor(212, 71, 94, 255);
                Color[ImGuiCol_HeaderActive] = ImColor(172, 31, 54, 255);
                break;
            case Color_::Green:
                Style.ChildRounding = 8.0f;
                Style.FrameRounding = 5.0f;

                Color[ImGuiCol_Button] = ImColor(10, 105, 56, 255);
                Color[ImGuiCol_ButtonHovered] = ImColor(30, 125, 76, 255);
                Color[ImGuiCol_ButtonActive] = ImColor(0, 95, 46, 255);

                Color[ImGuiCol_FrameBg] = ImColor(54, 54, 54, 150);
                Color[ImGuiCol_FrameBgActive] = ImColor(42, 42, 42, 150);
                Color[ImGuiCol_FrameBgHovered] = ImColor(100, 100, 100, 150);

                Color[ImGuiCol_CheckMark] = ImColor(10, 105, 56, 255);

                Color[ImGuiCol_SliderGrab] = ImColor(10, 105, 56, 255);
                Color[ImGuiCol_SliderGrabActive] = ImColor(0, 95, 46, 255);

                Color[ImGuiCol_Header] = ImColor(10, 105, 56, 255);
                Color[ImGuiCol_HeaderHovered] = ImColor(30, 125, 76, 255);
                Color[ImGuiCol_HeaderActive] = ImColor(0, 95, 46, 255);
                
                break;
            case Color_::Blue:
                Style.ChildRounding = 8.0f;
                Style.FrameRounding = 5.0f;

                Color[ImGuiCol_Button] = ImColor(51, 120, 255, 255);
                Color[ImGuiCol_ButtonHovered] = ImColor(71, 140, 255, 255);
                Color[ImGuiCol_ButtonActive] = ImColor(31, 100, 225, 255);

                Color[ImGuiCol_FrameBg] = ImColor(54, 54, 54, 150);
                Color[ImGuiCol_FrameBgActive] = ImColor(42, 42, 42, 150);
                Color[ImGuiCol_FrameBgHovered] = ImColor(100, 100, 100, 150);

                Color[ImGuiCol_CheckMark] = ImColor(51, 120, 255, 255);

                Color[ImGuiCol_SliderGrab] = ImColor(51, 120, 255, 255);
                Color[ImGuiCol_SliderGrabActive] = ImColor(31, 100, 225, 255);

                Color[ImGuiCol_Header] = ImColor(51, 120, 255, 255);
                Color[ImGuiCol_HeaderHovered] = ImColor(71, 140, 255, 255);
                Color[ImGuiCol_HeaderActive] = ImColor(31, 100, 225, 255);

                break;
            case Color_::Orange://233,87,33
                Style.ChildRounding = 8.0f;
                Style.FrameRounding = 5.0f;

                Color[ImGuiCol_Button] = ImColor(233, 87, 33, 255);
                Color[ImGuiCol_ButtonHovered] = ImColor(253, 107, 53, 255);
                Color[ImGuiCol_ButtonActive] = ImColor(213, 67, 13, 255);

                Color[ImGuiCol_FrameBg] = ImColor(54, 54, 54, 150);
                Color[ImGuiCol_FrameBgActive] = ImColor(42, 42, 42, 150);
                Color[ImGuiCol_FrameBgHovered] = ImColor(100, 100, 100, 150);

                Color[ImGuiCol_CheckMark] = ImColor(233, 87, 33, 255);

                Color[ImGuiCol_SliderGrab] = ImColor(233, 87, 33, 255);
                Color[ImGuiCol_SliderGrabActive] = ImColor(213, 67, 13, 255);

                Color[ImGuiCol_Header] = ImColor(233, 87, 33, 255);
                Color[ImGuiCol_HeaderHovered] = ImColor(253, 107, 53, 255);
                Color[ImGuiCol_HeaderActive] = ImColor(213, 67, 13, 255);

                break;
            }

            //初始化窗口
            if (WinPos) {
                ImGui::SetNextWindowPos({ float(Screen_Width - 960 * ScalingRatio) / 2,float(Screen_Heigth - 540 * ScalingRatio) / 2 });
                WinPos = false; //初始化完毕
            }

            ImGui::Begin("新大学生工具箱", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDocking);//开始绘制窗口
            ImGui::SetWindowSize({ 960.0f * ScalingRatio, 540.0f * ScalingRatio });//设置窗口大小

            {
                // 加横线
                /*ImGui::GetWindowDrawList()->AddLine({ ImGui::GetWindowPos().x + 430.0f,ImGui::GetWindowPos().y + 10.0f }, { ImGui::GetWindowPos().x + 430.0f,ImGui::GetWindowPos().y + 390.0f }, ImColor(100, 100, 100, 255));*/

                ImGui::SetCursorPos({ 30.0f * ScalingRatio,24.0f * ScalingRatio });
                ImGui::PushFont(Font_Title);
                ImGui::TextColored(Color[ImGuiCol_Button], "新大学生工具箱");
                ImGui::PopFont();

                ImGui::SetCursorPos({ 285.0f * ScalingRatio,18.0f * ScalingRatio });

                ImGui::PushStyleColor(ImGuiCol_Button, Tab == Tab::SetDevEnv ? Color[ImGuiCol_Button] : ImVec4(0.541f, 0.541f, 0.541f, 0.3f));
                if (ImGui::Button("部署环境", { 130.0f * ScalingRatio,40.0f * ScalingRatio }))
                    Tab = Tab::SetDevEnv;
                ImGui::PopStyleColor();

                ImGui::PushStyleColor(ImGuiCol_Button, Tab == Tab::Download ? Color[ImGuiCol_Button] : ImVec4(0.541f, 0.541f, 0.541f, 0.3f));
                ImGui::SetCursorPos({ 425.0f * ScalingRatio,18.0f * ScalingRatio });
                if (ImGui::Button("软件下载", { 130.0f * ScalingRatio,40.0f * ScalingRatio }))
                    Tab = Tab::Download;
                ImGui::PopStyleColor();

                ImGui::PushStyleColor(ImGuiCol_Button, Tab == Tab::SetSystem ? Color[ImGuiCol_Button] : ImVec4(0.541f, 0.541f, 0.541f, 0.3f));
                ImGui::SetCursorPos({ 565.0f * ScalingRatio,18.0f * ScalingRatio });
                if (ImGui::Button("系统设置", { 130.0f * ScalingRatio,40.0f * ScalingRatio }))
                    Tab = Tab::SetSystem;
                ImGui::PopStyleColor();

                ImGui::PushStyleColor(ImGuiCol_Button, Tab == Tab::SetToolbox ? Color[ImGuiCol_Button] : ImVec4(0.541f, 0.541f, 0.541f, 0.3f));
                ImGui::SetCursorPos({ 705.0f * ScalingRatio,18.0f * ScalingRatio });
                if (ImGui::Button("软件设置", { 130.0f * ScalingRatio,40.0f * ScalingRatio }))
                    Tab = Tab::SetToolbox;
                ImGui::PopStyleColor();

                ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.541f, 0.541f, 0.541f, 0.3f));
                ImGui::SetCursorPos({ 880.0f * ScalingRatio,18.0f * ScalingRatio });
                if (ImGui::Button("X", { 50.0f * ScalingRatio,40.0f * ScalingRatio }))
                    exit(0);
                ImGui::PopStyleColor();
            }

            ImGui::SetCursorPos({ 30.0f * ScalingRatio,70.0f * ScalingRatio });
            ImGui::BeginChild("软件设置", { 900.0f * ScalingRatio,435.0f * ScalingRatio }, true);
            switch (Tab) {
            case Tab::SetDevEnv:
                ImGui::PushFont(Font_Big);
                ImGui::BulletText("部署环境");
                ImGui::PopFont();
                ImGui::SameLine();
                Helpmarker("一键部署相应的环境，\n请确保本程序被完整下载。", Color[ImGuiCol_Button]);
                ImGui::Separator();
                //ImGui::TextColored(Color[ImGuiCol_Button], "\t待补充\n还没写");

                // 模块 - 安装 MinGW 环境 - 释放 1 次环境变量
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 50.0f * ScalingRatio });
                if (ImGui::Button("MinGW", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {  // 按钮被点击时返回true
                    int MinGW_CDE_A = CheckDevEnvironmentA("gcc --version", "gcc (x86_64-win32-seh-rev0, Built by MinGW-Builds project) 14.2.0", 5);
                    // 环境未安装
                    if (MinGW_CDE_A == 1)
                        MinGW_CDE_A_1 = true;
                    // 相同版本的环境已安装
                    else if (MinGW_CDE_A == 2)
                        MinGW_CDE_A_2 = true;
                    // 不同版本的环境已安装
                    else if (MinGW_CDE_A == 3)
                        MinGW_CDE_A_3 = true;
                    // 环境有无情况判断异常
                    else/* if (CDE_A == 4)*/
                        MinGW_CDE_A_4 = true;
                }
                ImGui::SetItemTooltip("一键安装C/C++的gcc和g++环境，\n请确保下载了该程序的完整内容。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 58.0f * ScalingRatio });
                ImGui::Text("C/C++ 开发环境");

                // 模块 - 安装 Python 环境 - 释放 2 次环境变量
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 93.0f * ScalingRatio });
                if (ImGui::Button("Python", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {  // 按钮被点击时返回true
                    int Python_CDE_A = CheckDevEnvironmentA("python --version", "Python 3.13.2", 6);
                    // 环境未安装
                    if (Python_CDE_A == 1)
                        Python_CDE_A_1 = true;
                    // 相同版本的环境已安装
                    else if (Python_CDE_A == 2)
                        Python_CDE_A_2 = true;
                    // 不同版本的环境已安装
                    else if (Python_CDE_A == 3)
                        Python_CDE_A_3 = true;
                    // 环境有无情况判断异常
                    else/* if (CDE_A == 4)*/
                        Python_CDE_A_4 = true;
                }
                ImGui::SetItemTooltip("一键安装Python环境，\n请确保下载了该程序的完整内容。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 101.0f * ScalingRatio });
                ImGui::Text("Python 开发环境");

                // 模块 - 安装 Java 环境 - 释放 1 次环境变量
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 136.0f * ScalingRatio });
                if (ImGui::Button("Java", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {  // 按钮被点击时返回true
                    int Java_CDE_A = CheckDevEnvironmentA("java --version", "java 21.0.6 2025-01-21 LTS", 7);
                    // 环境未安装
                    if (Java_CDE_A == 1)
                        Java_CDE_A_1 = true;
                    // 相同版本的环境已安装
                    else if (Java_CDE_A == 2)
                        Java_CDE_A_2 = true;
                    // 不同版本的环境已安装
                    else if (Java_CDE_A == 3)
                        Java_CDE_A_3 = true;
                    // 环境有无情况判断异常
                    else/* if (CDE_A == 4)*/
                        Java_CDE_A_4 = true;
                }
                ImGui::SetItemTooltip("一键安装Java21的JDK21环境，\n请确保下载了该程序的完整内容。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 144.0f * ScalingRatio });
                ImGui::Text("Java 开发环境");

                // 模块 - 安装 GoLang 环境 - 释放 1 次环境变量
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 179.0f * ScalingRatio });
                if (ImGui::Button("GoLang", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {  // 按钮被点击时返回true
                    int GoLang_CDE_A = CheckDevEnvironmentA("go version", "go version go1.24.1 windows/amd64", 10);
                    // 环境未安装
                    if (GoLang_CDE_A == 1)
                        GoLang_CDE_A_1 = true;
                    // 相同版本的环境已安装
                    else if (GoLang_CDE_A == 2)
                        GoLang_CDE_A_2 = true;
                    // 不同版本的环境已安装
                    else if (GoLang_CDE_A == 3)
                        GoLang_CDE_A_3 = true;
                    // 环境有无情况判断异常
                    else/* if (CDE_A == 4)*/
                        GoLang_CDE_A_4 = true;
                }
                ImGui::SetItemTooltip("一键安装GoLang环境，\n请确保下载了该程序的完整内容。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 187.0f * ScalingRatio });
                ImGui::Text("GoLang 开发环境");

                break;
            case Tab::Download:
                ImGui::PushFont(Font_Big);
                ImGui::BulletText("软件下载");
                ImGui::PopFont();
                ImGui::SameLine();
                Helpmarker("一键直达下载链接，\n请确保您已经连接网络。", Color[ImGuiCol_Button]);
                ImGui::Separator();

                // 模块 - 下载 Visual Studio Code
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 50.0f * ScalingRatio });
                if (ImGui::Button("VSCode", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://code.visualstudio.com/docs/?dv=win64user"); // 下载 VsCode
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                    
                ImGui::SetItemTooltip("直达官网，下载软件\nVisual Studio Code。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 58.0f * ScalingRatio });
                ImGui::Text("Visual Studio Code");

                // 模块 - 下载 Visual Studio Code
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 93.0f * ScalingRatio });
                if (ImGui::Button("MSVS", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=Community&channel=Release&version=VS2022&source=VSLandingPage&cid=2030&passive=false"); // 下载 Microsoft Visual Studio
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载软件\nMicrosoft Visual Studio。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 101.0f * ScalingRatio });
                ImGui::Text("Microsoft Visual Studio");

                // 模块 - 下载 Ubuntu
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 136.0f * ScalingRatio });
                if (ImGui::Button("Ubuntu", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://cn.ubuntu.com/download/desktop");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载系统文件\nUbuntu。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 144.0f * ScalingRatio });
                ImGui::Text("Linux下的发行版系统");

                // 模块 - 下载 Kali
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 179.0f * ScalingRatio });
                if (ImGui::Button("Kali", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://www.kali.org/get-kali/#kali-platforms");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载系统文件\nKali。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 187.0f * ScalingRatio });
                ImGui::Text("Linux下的发行版系统");

                // 模块 - 下载 WinRAR
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 222.0f * ScalingRatio });
                if (ImGui::Button("WinRAR", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://www.winrar.com.cn/index.htm");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载软件\nWinRAR。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 230.0f * ScalingRatio });
                ImGui::Text("压缩/解压软件");

                // 模块 - 下载 Bandizip
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 265.0f * ScalingRatio });
                if (ImGui::Button("Bandizip", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://www.bandisoft.com/bandizip/dl.php?all");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载软件\nBandizip。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 273.0f * ScalingRatio });
                ImGui::Text("压缩/解压软件");

                // 模块 - 下载 ToDesk
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 308.0f * ScalingRatio });
                if (ImGui::Button("ToDesk", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://www.todesk.com/download.html");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载软件\nToDesk。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 316.0f * ScalingRatio });
                ImGui::Text("远程协助软件");

                // 模块 - 下载 Steam
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 351.0f * ScalingRatio });
                if (ImGui::Button("Steam", { 120.0f * ScalingRatio,35.0f * ScalingRatio })) {
                    bool Download_Flag = Download_SW("https://store.steampowered.com/about/");
                    if (Download_Flag)
                        Download_Success = true;
                    else
                        Download_Fail = true;
                }
                ImGui::SetItemTooltip("直达官网，下载软件\nSteam。");
                ImGui::SetCursorPos({ 158.0f * ScalingRatio, 359.0f * ScalingRatio });
                ImGui::Text("高质量游戏平台");

                break;
            case Tab::SetSystem:
                ImGui::PushFont(Font_Big);
                ImGui::BulletText("系统设置");
                ImGui::PopFont();
                ImGui::SameLine();
                Helpmarker("一键修改系统设置", Color[ImGuiCol_Button]);
                ImGui::Separator();

                // 模块 - 类似于 Python 的长路径支持
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 62.0f * ScalingRatio });
                ImGui::Text("长路径支持");
                ImGui::SetCursorPos({ 130.0f * ScalingRatio, 60.0f * ScalingRatio });
                if (ImGui::Button("开启", { 50.0f * ScalingRatio,25.0f * ScalingRatio })) {
                    bool SetLongPath_ON = EnableLongPathSupport();
                    if (SetLongPath_ON)
                        SetLongPath_ON_Success = true;
                    else
                        SetLongPath_ON_Fail = true;
                }
                ImGui::SetCursorPos({ 190.0f * ScalingRatio, 60.0f * ScalingRatio });
                if (ImGui::Button("关闭", { 50.0f * ScalingRatio,25.0f * ScalingRatio })) {
                    bool SetLongPath_OFF = DisableLongPathSupport();
                    if (SetLongPath_OFF)
                        SetLongPath_OFF_Success = true;
                    else
                        SetLongPath_OFF_Fail = true;
                }
                
                //ImGui::InputInt("Int类型输入", &InputInt);
                //ImGui::InputFloat("Float类型输入", &InputFloat);
                //ImGui::InputText("char[]类型输入", &InputString[80], IM_ARRAYSIZE(InputString));
                //ImGui::SliderInt("Int类型滑块", &InputInt, 0, 100);
                //ImGui::SliderFloat("Float类型滑块", &InputFloat, 0.0F, 100.0F);
                //ImGui::Combo("选择框", &Comb, "A11\0A22\0A33\0A44");
                break;
            case Tab::SetToolbox:
                ImGui::PushFont(Font_Big);
                ImGui::BulletText("软件设置");
                ImGui::PopFont();
                ImGui::SameLine();
                Helpmarker("可设置本工具箱的一些功能", Color[ImGuiCol_Button]);
                ImGui::Separator();

                //ImGui::Checkbox("复选框-1", &CheckBox_1);
                //ImGui::Checkbox("复选框-2", &CheckBox_2);

                //ImGui::SetCursorPos({ 450.0f,330.0f });
                ImGui::SetCursorPos({ 25.0f * ScalingRatio, 62.0f * ScalingRatio });
                ImGui::Text("主题颜色");
                ImGui::SetCursorPos({ 112.0f * ScalingRatio, 60.0f * ScalingRatio });
                ImGui::SetNextItemWidth(80.0f * ScalingRatio);
                ImGui::Combo(" ", &Color_, "红色\0绿色\0蓝色\0橙色");

                //ImGui::SetCursorPos({ 122.0f * ScalingRatio,315.0f * ScalingRatio });
                //ImGui::TextColored(Color[ImGuiCol_Button], "免费版“新大学生工具箱”，QQ交流群：451662225，负责人QQ：167291828。");

                //ImGui::SetCursorPos({ 133.0f * ScalingRatio,338.0f * ScalingRatio });
                //ImGui::TextColored(Color[ImGuiCol_Button], "Team members: (11 members)\n\tTheXiaoLa, Binbim Tech, Xlorey, seven, violet, JiaYang, stars, Mushroom,\n\tpythonqwq, new chapter, tomorrow");

                //ImGui::SetCursorPos({ 88.0f * ScalingRatio,400.0f * ScalingRatio });
                //ImGui::TextColored(Color[ImGuiCol_Button], "Copyright 2025 TheXiaoLa's Team. \"New College Student Toolbox\" All rights reserved.");

                //time_t t = time(0);
                //char tmp[32] = { NULL };
                //strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M", localtime(&t));

                //ImGui::SetCursorPos({ 450.0f,365.0f });
                //ImGui::TextColored(Color[ImGuiCol_Button], "%s", tmp);
                break;
            }
            ImGui::EndChild();
            ImGui::End();
        }

        {
            // 子窗口
            // MinGW 区
            if (MinGW_CDE_A_1)
                CMD_MinGW_CDE_A_1(ScalingRatio, MinGW_CDE_A_1, MinGW_CDE_B_0, MinGW_UnZip_Wait,
                    MinGW_CDE_CF_1, MinGW_CDE_CF_0, mingwMutex, MinGW_In_UnZip_0,
                    MinGW_CDE_B_3, MinGW_CDE_B_4);
            if (MinGW_CDE_A_2)
                CMD_MinGW_CDE_A_2(ScalingRatio, MinGW_CDE_A_2);
            if (MinGW_CDE_A_3)
                CMD_MinGW_CDE_A_3(ScalingRatio, MinGW_CDE_A_3);
            if (MinGW_CDE_A_4)
                CMD_MinGW_CDE_A_4(ScalingRatio, MinGW_CDE_A_4);
            if (MinGW_CDE_B_0)
                CMD_MinGW_CDE_B_0(ScalingRatio, MinGW_CDE_B_0);
            if (MinGW_CDE_B_3)
                CMD_MinGW_CDE_B_3(ScalingRatio, MinGW_CDE_B_3);
            if (MinGW_CDE_B_4)
                CMD_MinGW_CDE_B_4(ScalingRatio, MinGW_CDE_B_4);
            if (MinGW_In_UnZip_0)
                CMD_MinGW_In_UnZip_0(ScalingRatio, MinGW_In_UnZip_0);
            if (MinGW_CDE_CF_0)
                CMD_MinGW_CDE_CF_0(ScalingRatio, MinGW_CDE_CF_0);
            if (MinGW_CDE_CF_1)
                CMD_MinGW_CDE_CF_1(ScalingRatio, MinGW_CDE_CF_1);

            // Python 区
            if (Python_CDE_A_1)
                CMD_Python_CDE_A_1(ScalingRatio, Python_CDE_A_1, Python_CDE_B_0, Python_UnZip_Wait,
                    Python_CDE_CF_1, Python_CDE_CF_0, PythonMutex, Python_In_UnZip_0,
                    Python_CDE_B_3, Python_CDE_B_4);
            if (Python_CDE_A_2)
                CMD_Python_CDE_A_2(ScalingRatio, Python_CDE_A_2);
            if (Python_CDE_A_3)
                CMD_Python_CDE_A_3(ScalingRatio, Python_CDE_A_3);
            if (Python_CDE_A_4)
                CMD_Python_CDE_A_4(ScalingRatio, Python_CDE_A_4);
            if (Python_CDE_B_0)
                CMD_Python_CDE_B_0(ScalingRatio, Python_CDE_B_0);
            if (Python_CDE_B_3)
                CMD_Python_CDE_B_3(ScalingRatio, Python_CDE_B_3);
            if (Python_CDE_B_4)
                CMD_Python_CDE_B_4(ScalingRatio, Python_CDE_B_4);
            if (Python_In_UnZip_0)
                CMD_Python_In_UnZip_0(ScalingRatio, Python_In_UnZip_0);
            if (Python_CDE_CF_0)
                CMD_Python_CDE_CF_0(ScalingRatio, Python_CDE_CF_0);
            if (Python_CDE_CF_1)
                CMD_Python_CDE_CF_1(ScalingRatio, Python_CDE_CF_1);

            // Java 区
            if (Java_CDE_A_1)
                CMD_Java_CDE_A_1(ScalingRatio, Java_CDE_A_1, Java_CDE_B_0, Java_UnZip_Wait,
                    Java_CDE_CF_1, Java_CDE_CF_0, JavaMutex, Java_In_UnZip_0,
                    Java_CDE_B_3, Java_CDE_B_4);
            if (Java_CDE_A_2)
                CMD_Java_CDE_A_2(ScalingRatio, Java_CDE_A_2);
            if (Java_CDE_A_3)
                CMD_Java_CDE_A_3(ScalingRatio, Java_CDE_A_3);
            if (Java_CDE_A_4)
                CMD_Java_CDE_A_4(ScalingRatio, Java_CDE_A_4);
            if (Java_CDE_B_0)
                CMD_Java_CDE_B_0(ScalingRatio, Java_CDE_B_0);
            if (Java_CDE_B_3)
                CMD_Java_CDE_B_3(ScalingRatio, Java_CDE_B_3);
            if (Java_CDE_B_4)
                CMD_Java_CDE_B_4(ScalingRatio, Java_CDE_B_4);
            if (Java_In_UnZip_0)
                CMD_Java_In_UnZip_0(ScalingRatio, Java_In_UnZip_0);
            if (Java_CDE_CF_0)
                CMD_Java_CDE_CF_0(ScalingRatio, Java_CDE_CF_0);
            if (Java_CDE_CF_1)
                CMD_Java_CDE_CF_1(ScalingRatio, Java_CDE_CF_1);

            // GoLang 区
            if (GoLang_CDE_A_1)
                CMD_GoLang_CDE_A_1(ScalingRatio, GoLang_CDE_A_1, GoLang_CDE_B_0, GoLang_UnZip_Wait,
                    GoLang_CDE_CF_1, GoLang_CDE_CF_0, GoLangMutex, GoLang_In_UnZip_0,
                    GoLang_CDE_B_3, GoLang_CDE_B_4);
            if (GoLang_CDE_A_2)
                CMD_GoLang_CDE_A_2(ScalingRatio, GoLang_CDE_A_2);
            if (GoLang_CDE_A_3)
                CMD_GoLang_CDE_A_3(ScalingRatio, GoLang_CDE_A_3);
            if (GoLang_CDE_A_4)
                CMD_GoLang_CDE_A_4(ScalingRatio, GoLang_CDE_A_4);
            if (GoLang_CDE_B_0)
                CMD_GoLang_CDE_B_0(ScalingRatio, GoLang_CDE_B_0);
            if (GoLang_CDE_B_3)
                CMD_GoLang_CDE_B_3(ScalingRatio, GoLang_CDE_B_3);
            if (GoLang_CDE_B_4)
                CMD_GoLang_CDE_B_4(ScalingRatio, GoLang_CDE_B_4);
            if (GoLang_In_UnZip_0)
                CMD_GoLang_In_UnZip_0(ScalingRatio, GoLang_In_UnZip_0);
            if (GoLang_CDE_CF_0)
                CMD_GoLang_CDE_CF_0(ScalingRatio, GoLang_CDE_CF_0);
            if (GoLang_CDE_CF_1)
                CMD_GoLang_CDE_CF_1(ScalingRatio, GoLang_CDE_CF_1);

            // 下载区
            if (Download_Success)
                CMD_Download_Success(ScalingRatio, Download_Success);
            if (Download_Fail)
                CMD_Download_Fail(ScalingRatio, Download_Fail);

            // 系统设置区
            if (SetLongPath_ON_Success)
                CMD_SetLongPath_ON_Success(ScalingRatio, SetLongPath_ON_Success);
            if (SetLongPath_ON_Fail)
                CMD_SetLongPath_ON_Fail(ScalingRatio, SetLongPath_ON_Fail);
            if (SetLongPath_OFF_Success)
                CMD_SetLongPath_OFF_Success(ScalingRatio, SetLongPath_OFF_Success);
            if (SetLongPath_OFF_Fail)
                CMD_SetLongPath_OFF_Fail(ScalingRatio, SetLongPath_OFF_Fail);
            if (MinGW_UnZip_Wait)
                CMD_MinGW_UnZip_Wait(ScalingRatio);
            if (Python_UnZip_Wait)
                CMD_Python_UnZip_Wait(ScalingRatio);
            if (Java_UnZip_Wait)
                CMD_Java_UnZip_Wait(ScalingRatio);
            if (GoLang_UnZip_Wait)
                CMD_GoLang_UnZip_Wait(ScalingRatio);
        }

        ImGui::Render();
        const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
        g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
        g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
        ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
            ImGui::UpdatePlatformWindows();
            ImGui::RenderPlatformWindowsDefault();
        }

        g_pSwapChain->Present(1, 0);

        auto now = std::chrono::steady_clock::now(); // 计算当前时间与下一帧目标时间的差值 - 降低性能占用
        // 如果有剩余时间，休眠直到下一帧开始
        if (now < next_frame_time)
            std::this_thread::sleep_until(next_frame_time);
    }

    ImGui_ImplDX11_Shutdown();
    ImGui_ImplWin32_Shutdown();
    ImGui::DestroyContext();

    CleanupDeviceD3D();
    ::DestroyWindow(hwnd);
    ::UnregisterClass(wc.lpszClassName, wc.hInstance);

    return 0;
}

bool CreateDeviceD3D(HWND hWnd) {
    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory(&sd, sizeof(sd));
    sd.BufferCount = 2;
    sd.BufferDesc.Width = 0;
    sd.BufferDesc.Height = 0;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;
    sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    UINT createDeviceFlags = 0;
    D3D_FEATURE_LEVEL featureLevel;
    const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
    if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
        return false;

    CreateRenderTarget();
    return true;
}

void CleanupDeviceD3D() {
    CleanupRenderTarget();
    if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
    if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
    if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
}

void CreateRenderTarget() {
    ID3D11Texture2D* pBackBuffer;
    g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
    g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
    pBackBuffer->Release();
}

void CleanupRenderTarget() {
    if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
}

#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0 // From Windows SDK 8.1+ headers
#endif

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
        return true;

    switch (msg) {
    case WM_SIZE:
        if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED) {
            CleanupRenderTarget();
            g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
            CreateRenderTarget();
        }
        return 0;
    case WM_SYSCOMMAND:
        if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
            return 0;
        break;
    case WM_DESTROY:
        ::PostQuitMessage(0);
        return 0;
    case WM_DPICHANGED:
        if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports) {
            const RECT* suggested_rect = (RECT*)lParam;
            ::SetWindowPos(hWnd, NULL, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE);
        }
        break;
    }
    return ::DefWindowProc(hWnd, msg, wParam, lParam);
}